﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.SoldierExam.BLL;

public partial class Aptitude_AptitudeStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            #region 权限
            bool isPermission = false;
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300300"))
            {
                isPermission = true;
            }
            else
            {
                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300311"))
                {
                    isPermission = true;
                }

            }
            if (!isPermission)
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            #endregion

            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            ddlExam.DataTextField = "Subject";
            ddlExam.DataValueField = "ExamID";
            ddlExam.DataSource = AptitudeExam.getList();
            ddlExam.DataBind();


            ddlRnaker.DataSource = AptitudeRanger.GetRanger();

            ddlRnaker.DataTextField = "Ranger";
            ddlRnaker.DataValueField = "RangerID";
            ddlRnaker.DataBind();
        }
    }

    private void Qurey()
    {
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        if (userInfo.DepartmentID.Substring(0, 4) == "1300")
        {
            gvStat.DataSource = Yipusoft.Account.BLL.Department.GetList("(SUBSTRING(DepartmentID, 5, 3) = '000') OR (DepartmentID = '1313100') OR (DepartmentID = '1312100')");
        }
        else
        {
            gvStat.DataSource = Yipusoft.Account.BLL.Department.GetList("(DepartmentID = '" + department.DepartmentID.Substring(0, 4) + "000')");

        }

        gvStat.DataBind();
        string caption = "";
        if (rblContranct.SelectedValue == "0")
        {
            caption = "消防官兵";
        }
        else if (rblContranct.SelectedValue == "1")
        {
            caption = "合同制消防员";
        }
        else if (rblContranct.SelectedValue == "-1")
        {
            caption = "全部士兵";
        }

        if (ddlRnaker.SelectedValue != "0")
        {
            caption += ddlRnaker.SelectedItem.Text;
        }

        caption = "<span>" + caption + "考核统计</span>";
        gvStat.Caption = caption;

    }
    #region//总数
    public string GetSum(string departmentID)
    {
        if (ddlRnaker.SelectedValue == "0")//所有资格
        {
            //合同制
            if (rblContranct.SelectedValue == "-1")
            {
                return AptitudeSoldier.GetSoldierCountByRanger(departmentID).ToString();
            }
            else
            {
                bool isContranct = false;
                if (rblContranct.SelectedValue == "0")
                {
                    isContranct = false;
                }
                else
                {
                    isContranct = true;
                }
                return AptitudeSoldier.GetSoldierCountByRanger(departmentID, isContranct).ToString();
            }
        }
        else
        {
            //合同制
            if (rblContranct.SelectedValue == "-1")
            {
                return AptitudeSoldier.GetSoldierCountByRanger(departmentID, Convert.ToInt32(ddlRnaker.SelectedValue)).ToString();
            }
            else
            {
                bool isContranct = false;
                if (rblContranct.SelectedValue == "0")
                {
                    isContranct = false;
                }
                else
                {
                    isContranct = true;
                }
                return AptitudeSoldier.GetSoldierCountByRanger(departmentID, Convert.ToInt32(ddlRnaker.SelectedValue), isContranct).ToString();
            }
        }
    }

    #endregion

    #region //合格
    public string GetOKCount(string departmentID)
    {
        if (ddlRnaker.SelectedValue != "0")
        {
            if (rblContranct.SelectedValue == "-1")
            {
                return AptitudeSoldier.GetStandardSoldierCount(departmentID, Convert.ToInt32(ddlRnaker.SelectedValue), Convert.ToInt32(ddlExam.SelectedValue), 1).ToString();
            }
            else
            {
                bool isContranct = false;
                if (rblContranct.SelectedValue == "0")
                {
                    isContranct = false;
                }
                else
                {
                    isContranct = true;
                }
                return AptitudeSoldier.GetStandardSoldierCount(departmentID, Convert.ToInt32(ddlRnaker.SelectedValue), 1, Convert.ToInt32(ddlExam.SelectedValue), isContranct).ToString();
            }
        }
        else
        {
            if (rblContranct.SelectedValue == "-1")
            {
                return AptitudeSoldier.GetStandardSoldierCount(departmentID, Convert.ToInt32(ddlExam.SelectedValue), 1).ToString();
            }
            else
            {
                bool isContranct = false;
                if (rblContranct.SelectedValue == "0")
                {
                    isContranct = false;
                }
                else
                {
                    isContranct = true;
                }
                return AptitudeSoldier.GetStandardSoldierCount(departmentID, Convert.ToInt32(ddlExam.SelectedValue), 1, isContranct).ToString();
            }
        }
    }

    #endregion


    #region 考核
    public string GetExamCount(string departmentID)
    {
        if (ddlRnaker.SelectedValue != "0")
        {
            if (rblContranct.SelectedValue == "-1")
            {
                return AptitudeSoldier.GetExamSoldierCount(departmentID, Convert.ToInt32(ddlRnaker.SelectedValue), Convert.ToInt32(ddlExam.SelectedValue)).ToString();
            }
            else
            {
                bool isContranct = false;
                if (rblContranct.SelectedValue == "0")
                {
                    isContranct = false;
                }
                else
                {
                    isContranct = true;
                }
                return AptitudeSoldier.GetExamSoldierCount(departmentID, Convert.ToInt32(ddlRnaker.SelectedValue), Convert.ToInt32(ddlExam.SelectedValue), isContranct).ToString();
            }
        }
        else
        {
            if (rblContranct.SelectedValue == "-1")
            {
                return AptitudeSoldier.GetExamSoldierCount(departmentID, Convert.ToInt32(ddlExam.SelectedValue)).ToString();
            }
            else
            {
                bool isContranct = false;
                if (rblContranct.SelectedValue == "0")
                {
                    isContranct = false;
                }
                else
                {
                    isContranct = true;
                }
                return AptitudeSoldier.GetExamSoldierCount(departmentID, Convert.ToInt32(ddlExam.SelectedValue), isContranct).ToString();
            }
        }
    }
    #endregion
    protected void gvStat_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection tcl = e.Row.Cells;
            //清除自动生成的表头
            tcl.Clear();

            //添加新的表头
            tcl.Add(new TableHeaderCell());
            tcl[0].Text = "单位";
            tcl[0].Attributes.Add("style", "border-color:#CEE7F7;");


            tcl.Add(new TableHeaderCell());
            tcl[1].Text = "总数";

            tcl[1].Attributes.Add("style", "border-color:#CEE7F7;");


            tcl.Add(new TableHeaderCell());

            tcl[2].Text = "考核";

            tcl[2].Attributes.Add("style", "border-color:#CEE7F7;");

            tcl.Add(new TableHeaderCell());
            tcl[3].Text = "合格";
            tcl[3].Attributes.Add("style", "border-color:#CEE7F7;");

            tcl.Add(new TableHeaderCell());
            tcl[4].Text = "参考率";
            tcl[4].Attributes.Add("style", "border-color:#CEE7F7;");

            tcl.Add(new TableHeaderCell());
            tcl[5].Text = "合格率";
            tcl[5].Attributes.Add("style", "border-color:#CEE7F7;");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DepartmentInfo department = e.Row.DataItem as DepartmentInfo;
            //部门ID
            string departmentid = department.DepartmentID;
            //Examid
            int Examid = Convert.ToInt32(ddlExam.SelectedValue);
            //RankerID
            int RankerID = Convert.ToInt32(ddlRnaker.SelectedValue);
            //Contranct
            int Contranct = Convert.ToInt32(rblContranct.SelectedValue);

            Label lblSum = e.Row.FindControl("lblSum") as Label;
            Label lblExam = e.Row.FindControl("lblExam") as Label;
            HyperLink lblOk = e.Row.FindControl("lblOk") as HyperLink;
            Label lblCanKaolu = e.Row.FindControl("lblCanKaolu") as Label;
            Label lblOKLu = e.Row.FindControl("lblOKLu") as Label;
            lblSum.Text = GetSum(departmentid);
            lblExam.Text = GetExamCount(departmentid);
            lblOk.Text = GetOKCount(departmentid);
            lblOk.NavigateUrl = "SearchAptitude.aspx?Where=1&Departmentid=" + departmentid + "&Standard=1&Examid=" + Examid + "&RankerID=" + RankerID + "&Contranct=" + Contranct;
            if (Convert.ToInt32(lblSum.Text) > 0)
            {
                decimal cc = Convert.ToDecimal(lblExam.Text) / Convert.ToDecimal(lblSum.Text) * 100;
                lblCanKaolu.Text = cc.ToString("0.00") + "%";
            }
            if (Convert.ToInt32(lblExam.Text) > 0)
            {
                decimal pp = Convert.ToDecimal(lblOk.Text) / Convert.ToDecimal(lblExam.Text) * 100;
                lblOKLu.Text = pp.ToString("0.00") + "%";
            }
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        Qurey();
    }
}
